home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter2 / printdlg.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  10KB  |  280 lines

  1.  
  2. #include <windows.h>  
  3. #include "printdlg.h"  
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName = "MyApp";
  19. LPCTSTR lpszTitle   = "PrintDlg()"; 
  20.  
  21.  
  22. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  23.  
  24.  
  25. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  26.                       LPTSTR lpCmdLine, int nCmdShow)
  27. {
  28.    MSG      msg;
  29.    HWND     hWnd; 
  30.    WNDCLASS wc;
  31.  
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    hWnd = CreateWindow( lpszAppName, 
  54.                         lpszTitle,    
  55.                         WS_OVERLAPPEDWINDOW, 
  56.                         CW_USEDEFAULT, 0, 
  57.                         CW_USEDEFAULT, 0,  
  58.                         NULL,              
  59.                         NULL,              
  60.                         hInstance,         
  61.                         NULL               
  62.                       );
  63.  
  64.    if ( !hWnd ) 
  65.       return( FALSE );
  66.  
  67.    ShowWindow( hWnd, nCmdShow ); 
  68.    UpdateWindow( hWnd );         
  69.  
  70.    while( GetMessage( &msg, NULL, 0, 0) )   
  71.    {
  72.       TranslateMessage( &msg ); 
  73.       DispatchMessage( &msg );  
  74.    }
  75.  
  76.    return( msg.wParam ); 
  77. }
  78.  
  79.  
  80. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  81. {
  82.    WNDCLASSEX wcex;
  83.  
  84.    wcex.style         = lpwc->style;
  85.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  86.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  87.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  88.    wcex.hInstance     = lpwc->hInstance;
  89.    wcex.hIcon         = lpwc->hIcon;
  90.    wcex.hCursor       = lpwc->hCursor;
  91.    wcex.hbrBackground = lpwc->hbrBackground;
  92.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  93.    wcex.lpszClassName = lpwc->lpszClassName;
  94.  
  95.    // Added elements for Windows 95.
  96.    //...............................
  97.    wcex.cbSize = sizeof(WNDCLASSEX);
  98.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  99.                             IMAGE_ICON, 16, 16,
  100.                             LR_DEFAULTCOLOR );
  101.             
  102.    return RegisterClassEx( &wcex );
  103. }
  104.  
  105. LPCTSTR lpszText = "This sample text will be printed to the printer.";
  106.  
  107. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  108. {
  109. static PAGESETUPDLG psd;
  110. static HWND         hEdit = NULL;
  111.  
  112.    switch( uMsg )
  113.    {
  114.       case WM_CREATE :
  115.               psd.lStructSize     = sizeof( PAGESETUPDLG );
  116.               psd.hwndOwner       = hWnd;
  117.               psd.rtMargin.top    = 1000;
  118.               psd.rtMargin.left   = 1000;
  119.               psd.rtMargin.bottom = 1000;
  120.               psd.rtMargin.right  = 1000;  
  121.               psd.hDevMode        = GlobalAlloc( GHND, sizeof( DEVMODE ) );
  122.               psd.hDevNames       = GlobalAlloc( GHND, sizeof( DEVNAMES ) );
  123.               psd.Flags           = PSD_MARGINS | PSD_INTHOUSANDTHSOFINCHES; 
  124.  
  125.               hEdit = CreateWindow( "EDIT", "",    
  126.                                     WS_CHILD | ES_LEFT | 
  127.                                     WS_VISIBLE | ES_MULTILINE | ES_NOHIDESEL, 
  128.                                     0, 0, 
  129.                                     0, 0,  
  130.                                     hWnd,              
  131.                                     (HMENU)101,              
  132.                                     hInst,         
  133.                                     NULL               
  134.                                   );
  135.  
  136.               SendMessage( hEdit, WM_SETTEXT, 0, (LPARAM)lpszText );
  137.               break;
  138.  
  139.       case WM_SIZE :
  140.               MoveWindow( hEdit, 0, 0, LOWORD( lParam ), HIWORD( lParam ), TRUE );
  141.               break;
  142.  
  143.       case WM_COMMAND :
  144.               switch( LOWORD( wParam ) )
  145.               {
  146.                  case IDM_PAGESETUP :
  147.                         PageSetupDlg( &psd );
  148.                         break;
  149.  
  150.                  case IDM_PRINT :
  151.                         {
  152.                            PRINTDLG pd;
  153.  
  154.                            // Initialize the PRINTDLG structure.
  155.                            //...................................
  156.                            memset( &pd, 0, sizeof( PRINTDLG ) );
  157.                            pd.lStructSize    = sizeof( PRINTDLG );
  158.                            pd.hwndOwner      = hWnd;
  159.                            pd.Flags          = PD_RETURNDC;
  160.                            pd.hDevMode       = psd.hDevMode;
  161.                            pd.hDevNames      = psd.hDevNames;
  162.  
  163.                            // Create the print dialog.
  164.                            //.........................
  165.                            if ( PrintDlg( &pd ) )
  166.                            {
  167.                               WORD      wCopies;
  168.                               char      szBuf[512];
  169.                               RECT      rect;
  170.                               DOCINFO   di;
  171.                               LPDEVMODE lpDevMode = (LPDEVMODE)GlobalLock( pd.hDevMode );
  172.  
  173.                               memset( &di, 0, sizeof( DOCINFO ) );
  174.  
  175.                               // Initialize rect to size of page.
  176.                               //.................................
  177.                               rect.top = 
  178.                                      ( GetDeviceCaps( pd.hDC, LOGPIXELSY )*
  179.                                      (psd.rtMargin.top/1000 ))-
  180.                                      GetDeviceCaps( pd.hDC, PHYSICALOFFSETY );
  181.  
  182.                               rect.left = 
  183.                                      ( GetDeviceCaps( pd.hDC, LOGPIXELSX )*
  184.                                      (psd.rtMargin.left/1000 ))-
  185.                                      GetDeviceCaps( pd.hDC, PHYSICALOFFSETX );
  186.  
  187.                               rect.right = 
  188.                                      lpDevMode->dmPelsWidth - 
  189.                                      ( GetDeviceCaps( pd.hDC, LOGPIXELSX )*
  190.                                      (psd.rtMargin.right/1000 ))-
  191.                                      GetDeviceCaps( pd.hDC, PHYSICALOFFSETX );
  192.  
  193.                               rect.bottom = 
  194.                                      lpDevMode->dmPelsHeight - 
  195.                                      ( GetDeviceCaps( pd.hDC, LOGPIXELSY )*
  196.                                      (psd.rtMargin.bottom/1000 ))-
  197.                                      GetDeviceCaps( pd.hDC, PHYSICALOFFSETY );
  198.  
  199.                               // Initialize di with the document title.
  200.                               //.......................................
  201.                               di.cbSize      = sizeof( DOCINFO );
  202.                               di.lpszDocName = lpszTitle;
  203.  
  204.                               // Retrieve the text from the edit control.
  205.                               //.........................................
  206.                               SendMessage( hEdit, WM_GETTEXT, sizeof( szBuf ), (LPARAM)szBuf );
  207.  
  208.                               // Print the text.
  209.                               //................
  210.                               if ( StartDoc( pd.hDC, &di ) > 0 )
  211.                               {
  212.                                  for( wCopies = 0; wCopies < pd.nCopies; wCopies++ )
  213.                                  {
  214.                                     StartPage( pd.hDC );
  215.                                     DrawText( pd.hDC, szBuf, strlen( szBuf ), &rect, DT_LEFT | DT_WORDBREAK );
  216.                                     EndPage( pd.hDC );
  217.                                  }
  218.  
  219.                                  EndDoc( pd.hDC );
  220.                               }
  221.  
  222.                               // Unlock and free resources.
  223.                               //...........................
  224.                               GlobalUnlock( pd.hDevMode );
  225.                               DeleteDC( pd.hDC ); 
  226.                            }
  227.                            else if ( CommDlgExtendedError() == PDERR_NODEFAULTPRN )
  228.                               MessageBox( hWnd, "No default printer selected.", 
  229.                                                 "Print", MB_OK | MB_ICONSTOP );
  230.                         }
  231.                         break;
  232.  
  233.                  case IDM_ABOUT :
  234.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  235.                         break;
  236.  
  237.                  case IDM_EXIT :
  238.                         DestroyWindow( hWnd );
  239.                         break;
  240.               }
  241.               break;
  242.  
  243.       case WM_DESTROY :
  244.               GlobalFree( psd.hDevMode );
  245.               GlobalFree( psd.hDevNames );
  246.  
  247.               PostQuitMessage(0);
  248.               break;
  249.  
  250.       default :
  251.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  252.    }
  253.  
  254.    return( 0L );
  255. }
  256.  
  257.  
  258. LRESULT CALLBACK About( HWND hDlg,           
  259.                         UINT message,        
  260.                         WPARAM wParam,       
  261.                         LPARAM lParam)
  262. {
  263.    switch (message) 
  264.    {
  265.        case WM_INITDIALOG: 
  266.                return (TRUE);
  267.  
  268.        case WM_COMMAND:                              
  269.                if (   LOWORD(wParam) == IDOK         
  270.                    || LOWORD(wParam) == IDCANCEL)    
  271.                {
  272.                        EndDialog(hDlg, TRUE);        
  273.                        return (TRUE);
  274.                }
  275.                break;
  276.    }
  277.  
  278.    return (FALSE); 
  279. }
  280.